<?php

namespace pmvc\remoting\jsonrpc;

use pmvc\aop\MethodInterceptor;
use pmvc\ioc\InitializingObject;
use pmvc\remoting\HTTPRemotingClientFactoryObject;
use Exception;

/**
 * An {@link HTTPRemotingClientFactoryObject} that uses the
 * {@link JsonRpcClient} to create a service proxy.
 */
class JsonRpcRemotingClientFactoryObject
	extends HTTPRemotingClientFactoryObject
	implements InitializingObject,
	MethodInterceptor {

	private $jsonRpcClient;
	private $headers = Array();

	/**
	 * {@inheritDoc}
	 */
	public function initialize() {
		$this->jsonRpcClient = new JsonRpcClient($this->getUrl());
	}

	/**
	 * {@inheritDoc}
	 */
	protected function createMethodInterceptor() {
		return $this;
	}

	/**
	 * {@inheritDoc}
	 */
	public function methodInvoked($object, $methodName, &$args, $userData=NULL) {
		return $this->jsonRpcClient->invoke($methodName, $args, FALSE, $this->headers);
	}

	/**
	 * Extra headers to be included with every request.
	 * @param array $headers the headers
	 */
	public function setHeader(Array $headers) {
		$this->headers = $headers;
	}

}

?>